package com.team.arms.yuding.controller;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.team.arms.common.PageVO;
import com.team.arms.common.annotation.Log;
import com.team.arms.common.ry.core.controller.BaseController;
import com.team.arms.common.ry.core.domain.AjaxResult;
import com.team.arms.common.ry.enums.BusinessType;
import com.team.arms.yuding.domain.LotteryTable;
import com.team.arms.yuding.service.LotteryTableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yjh
 * @since 2022-04-27
 */
@Api( tags = "摇号信息")
@RestController
@RequestMapping("/reservesoldiers/lottery")
public class LotteryTableController extends BaseController {


    @Autowired
    private LotteryTableService lotteryTableService;

    @ApiOperation("查询摇号信息")
    @GetMapping("/selectlottery")
    public AjaxResult selectLottery(@ApiParam("选中的页数") @RequestParam("pageNum") Integer pageNum ,
                                    @ApiParam("页面显示的数据量") @RequestParam("pageSize") Integer pageSize){

        PageHelper.startPage(pageNum,pageSize);
        List<LotteryTable> list = lotteryTableService.list();
        PageInfo<LotteryTable> pageInfo = new PageInfo<LotteryTable>(list);
        PageVO<LotteryTable> lotteryTablePageVO = new PageVO<>();
        BeanUtils.copyProperties(pageInfo, lotteryTablePageVO);
        lotteryTablePageVO.setResult(pageInfo.getList());
        lotteryTablePageVO.setPage(pageNum);
        return AjaxResult.success(lotteryTablePageVO);
    }

    @ApiOperation("根据id查询摇号信息")
    @GetMapping("/selectById")
    public AjaxResult selectById(@RequestParam(value = "lottery_number")String id){
        return AjaxResult.success(lotteryTableService.getById(id));
    }

    @ApiOperation("修改摇号信息")
    @PutMapping("updatelottery")
    public AjaxResult updateLottery(
            @RequestParam(value = "lottery_number")String id,
            @RequestBody LotteryTable lotteryTable){

        QueryWrapper<LotteryTable> wrapper = new QueryWrapper();
        wrapper.eq("lottery_number",id);
        LotteryTable byId = lotteryTableService.getById(id);
        return toAjax(lotteryTableService.update(lotteryTable,wrapper));
    }
    /**
     * 导出摇号信息列表
     */
    @ApiOperation("导出摇号信息")
    @Log(title = "摇号信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response)
    {
        //设置下载信息
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //这里URLEncoder.encode可以防止中文乱码
        String fileName = "test1";
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        //查询数据库
        List<LotteryTable> lotteryTables = lotteryTableService.list();
        //调用方法进行写操作
        try {
            EasyExcel.write(response.getOutputStream(), LotteryTable.class).sheet("摇号信息")
                    .doWrite(lotteryTables);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

